package com.gengdan.service.impl;

import com.gengdan.bean.*;
import com.gengdan.dao.SampleStorageDao;
import com.gengdan.service.SampleStorageService;
import com.gengdan.util.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.*;

@Service
public class SampleStorageServiceImpl implements SampleStorageService {
    @Autowired
    private SampleStorageDao sampleStorageDao;

    /***
    * @Description 查询样本入库【捐献者】
    * @param userId
    * @Return java.util.HashMap<java.lang.String,java.lang.Object>
    * @Author Sun Mingshan
    * @Date 2020/5/10 11:05
    **/
    public HashMap<String, Object> selectSampleStorage(int userId) {
        HashMap<String,Object> hashMap = new HashMap<String, Object>();
        List<SampleStorage> SampleStorageList = sampleStorageDao.selectSampleStorage(userId);

        if(SampleStorageList.isEmpty()){
            hashMap.put("status","0");
            hashMap.put("msg","查询失败");
            hashMap.put("select_sampleStorage",SampleStorageList);
            return hashMap;
        }

        hashMap.put("status","1");
        hashMap.put("msg","查询成功");
        hashMap.put("select_sampleStorage",SampleStorageList);
        return hashMap;
    }

    /***
    * @Description 入库记录查询【入库员】
    * @param
    * @Return java.util.HashMap<java.lang.String,java.lang.Object>
    * @Author Sun Mingshan
    * @Date 2020/5/10 10:59
    **/
    @Override
    public HashMap<String, Object> selectSampleStorageR2(Page page) {
        HashMap<String,Object> hashMap = new HashMap<String, Object>();
        //设置起始页 条数
        com.github.pagehelper.Page<Object> pageHelper= PageHelper.startPage(page.getStart(),page.getCount());
        List<SampleStorageR2> SampleStorageListR2 = sampleStorageDao.selectSampleStorageR2();
        //设置末尾页（第一次加载生效）
        long total = pageHelper.getTotal();
        page.caculateLast(total);

        if(SampleStorageListR2.isEmpty()){
            hashMap.put("status","0");
            hashMap.put("msg","查询失败");
            hashMap.put("select_sampleStorageR2",SampleStorageListR2);
            return hashMap;
        }

        hashMap.put("status","1");
        hashMap.put("msg","查询成功");
        hashMap.put("select_sampleStorageR2",SampleStorageListR2);
        return hashMap;
    }
    /***
    * @Description 删除历史记录【入库表】
    * @param map
    * @Return java.util.HashMap<java.lang.String,java.lang.Object>
    * @Author Sun Mingshan
    * @Date 2020/5/13 18:16
    **/
    @Override
    @Transactional(propagation= Propagation.REQUIRED,rollbackForClassName="Exception")
    public HashMap<String, Object> deleteSampleStorageR2(Map map) {
        HashMap<String,Object> hashMap = new HashMap<String, Object>();
        int SampleStorageListR2 = 0;
        for(int i = 1;i<=map.size();i++){
            String value = "delCountryId";
            //拼接
            String number = Integer.toString(i);
            String result = value.concat(number);
            System.out.println(result);
            String delIndex = (String) map.get(result);
            System.out.println(delIndex);
            //执行删除
            SampleStorageListR2 = sampleStorageDao.deleteSampleStorageR2(delIndex);
            if(SampleStorageListR2==0){
                hashMap.put("status","0");
                hashMap.put("msg","删除失败");
                //回滚
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return hashMap;

            }
        }
        hashMap.put("status","1");
        hashMap.put("msg","删除成功");
        return hashMap;
    }
    /***
    * @Description 【入库历史记录】高级检索
    * @param page
    * @param keyWord
    * @Return java.util.HashMap<java.lang.String,java.lang.Object>
    * @Author Sun Mingshan
    * @Date 2020/5/13 17:36
    **/
    @Override
    public HashMap<String, Object> selectSampleStorageR2ByKey(Page page, String keyWord) {
        HashMap<String, Object> hashMap = new HashMap<String, Object>();
        com.github.pagehelper.Page<Object> pageHelper= PageHelper.startPage(page.getStart(),page.getCount());
        //        排序
        //        PageHelper.orderBy("id ASC");
        String indexKeyWord = "%"+keyWord+"%";
        List<SampleStorageR2> SampleStorageMsg = sampleStorageDao.selectSampleStorageR2ByKey(indexKeyWord);
        long total = pageHelper.getTotal();
        page.caculateLast(total);


        if(SampleStorageMsg.isEmpty()){
            hashMap.put("status","0");
            hashMap.put("msg","查询失败");
            hashMap.put("SampleStorageMsg",SampleStorageMsg);
            return hashMap;
        }

        hashMap.put("status","1");
        hashMap.put("msg","查询成功");
        hashMap.put("SampleStorageMsg",SampleStorageMsg);
        return hashMap;
    }

    /***
    * @Description 1.查找容器【入库员】
    * @param
    * @Return java.util.HashMap<java.lang.String,java.lang.Object>
    * @Author Sun Mingshan
    * @Date 2020/5/10 15:21
    **/
    @Override
    public  HashMap<String,Object> selectContainerName() {
        HashMap<String,Object> hashMap = new HashMap<String, Object>();
        List<ContainerInfo> containerInfos = sampleStorageDao.selectContainerName();
        if(containerInfos.isEmpty()){
            hashMap.put("status","0");
            hashMap.put("msg","查询失败");
            hashMap.put("containerInfos",containerInfos);
            return hashMap;
        }

        hashMap.put("status","1");
        hashMap.put("msg","查询成功");
        hashMap.put("containerInfos",containerInfos);
        return hashMap;
    }
    /***
    * @Description 2.查找存储区【入库员】
    * @param conName
    * @Return java.util.HashMap<java.lang.String,java.lang.Object>
    * @Author Sun Mingshan
    * @Date 2020/5/10 15:29
    **/
    @Override
    public HashMap<String, Object> selectStorageArea(String conName) {
        HashMap<String,Object> hashMap = new HashMap<String, Object>();
        List<HoleLocInfo> StorageArea = sampleStorageDao.selectStorageArea(conName);
        if(StorageArea.isEmpty()){
            hashMap.put("status","0");
            hashMap.put("msg","查询失败");
            hashMap.put("StorageArea",StorageArea);
            return hashMap;
        }
        hashMap.put("status","1");
        hashMap.put("msg","查询成功");
        hashMap.put("StorageArea",StorageArea);
        return hashMap;
    }

    /***
    * @Description 3.查找冻存盒【入库员】
    * @param holeLocCode
    * @Return java.util.HashMap<java.lang.String,java.lang.Object>
    * @Author Sun Mingshan
    * @Date 2020/5/10 15:37
    **/
    @Override
    public HashMap<String, Object> selectHoleLocCode(String holeLocCode) {
        HashMap<String,Object> hashMap = new HashMap<String, Object>();
        String searchHoleLocCode = holeLocCode+"%";
        List<HoleLocInfo> holeLocInfos = sampleStorageDao.selectHoleLocCode(searchHoleLocCode);

        if(holeLocInfos.isEmpty()){
            hashMap.put("status","0");
            hashMap.put("msg","查询失败");
            hashMap.put("holeLocInfos",holeLocInfos);
            return hashMap;
        }
        hashMap.put("status","1");
        hashMap.put("msg","查询成功");
        hashMap.put("holeLocInfos",holeLocInfos);
        return hashMap;
    }

    /***
    * @Description 查找样本处理信息【登记入库板块】
    * @param
    * @Return java.util.HashMap<java.lang.String,java.lang.Object>
    * @Author Sun Mingshan
    * @Date 2020/5/11 12:39
    **/
    @Override
    public HashMap<String, Object> selectSampleProcR2(Page page) {
        HashMap<String, Object> hashMap = new HashMap<String, Object>();
        com.github.pagehelper.Page<Object> pageHelper= PageHelper.startPage(page.getStart(),page.getCount());
        //        排序
        //        PageHelper.orderBy("id ASC");
        List<SampleProcR2> SampleProcMsg = sampleStorageDao.selectSampleProcR2();
        long total = pageHelper.getTotal();
        page.caculateLast(total);

        if(SampleProcMsg.isEmpty()){
            hashMap.put("status","0");
            hashMap.put("msg","查询失败");
            hashMap.put("SampleProcMsg",SampleProcMsg);
            return hashMap;
        }

        hashMap.put("status","1");
        hashMap.put("msg","查询成功");
        hashMap.put("SampleProcMsg",SampleProcMsg);
        return hashMap;
    }

    /***
    * @Description 删除处理的样品【登记入库板块】
    * @param map
    * @Return java.util.HashMap<java.lang.String,java.lang.Object>
    * @Author Sun Mingshan
    * @Date 2020/5/11 12:53
    **/
    @Override
    public HashMap<String, Object> deleteSampleProc(Map map) {
        HashMap<String,Object> hashMap = new HashMap<String, Object>();
        int deleteSampleProcResult = 0;
        for(int i = 1;i<=map.size();i++){
            String value = "delCountryId";
            //拼接
            String number = Integer.toString(i);
            String result = value.concat(number);
            System.out.println(result);
            String delIndex = (String) map.get(result);
            System.out.println(delIndex);
            //执行删除
            deleteSampleProcResult = sampleStorageDao.deleteSampleProc(delIndex);
            if(deleteSampleProcResult==0){
                hashMap.put("status","0");
                hashMap.put("msg","删除失败");
                //回滚
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return hashMap;

            }
        }
        hashMap.put("status","1");
        hashMap.put("msg","删除成功");
        return hashMap;

    }

    /***
    * @Description 高级检索样本处理信息【登记入库板块】
    * @param page
    * @param keyWord
    * @Return java.util.HashMap<java.lang.String,java.lang.Object>
    * @Author Sun Mingshan
    * @Date 2020/5/11 12:56
    **/
    @Override
    public HashMap<String, Object> selectSampleProcR2ByKey(Page page,String keyWord) {
        HashMap<String, Object> hashMap = new HashMap<String, Object>();
        com.github.pagehelper.Page<Object> pageHelper= PageHelper.startPage(page.getStart(),page.getCount());
        //        排序
        //        PageHelper.orderBy("id ASC");
        List<SampleProcR2> SampleProcMsgBykey = sampleStorageDao.selectSampleProcR2ByKey(keyWord);
        long total = pageHelper.getTotal();
        page.caculateLast(total);

        if(SampleProcMsgBykey.isEmpty()){
            hashMap.put("status","0");
            hashMap.put("msg","查询失败");
            hashMap.put("SampleProcMsgBykey",SampleProcMsgBykey);
            return hashMap;
        }

        hashMap.put("status","1");
        hashMap.put("msg","查询成功");
        hashMap.put("SampleProcMsgBykey",SampleProcMsgBykey);
        return hashMap;
    }

    /***
    * @Description 更新处理样品状态【登记入库板块】
    * @param countryCode
    * @Return java.util.HashMap<java.lang.String,java.lang.Object>
    * @Author Sun Mingshan
    * @Date 2020/5/11 13:22
    **/
    @Override
    public HashMap<String, Object> updateSampleProc(int countryCode) {
        HashMap<String,Object> hashMap = new HashMap<String, Object>();
        int updateSampleProcResult = sampleStorageDao.updateSampleProc(countryCode);
        System.out.println("updateSampleProc结果"+updateSampleProcResult);
        if(updateSampleProcResult==0){
            hashMap.put("status","0");
            hashMap.put("msg","更新失败");
            return hashMap;
        }
        hashMap.put("status","1");
        hashMap.put("msg","更新成功");
        return hashMap;
    }
    /***
    * @Description 插入
    * @param map
    * @Return java.util.HashMap<java.lang.String,java.lang.Object>
    * @Author Sun Mingshan
    * @Date 2020/5/12 15:06
    **/
    @Override
    public HashMap<String, Object> insertSampleStorage(Map map) {
        HashMap hashMap = new HashMap();
        int returnResult = 0;
        for(int i = 1;i<=map.size();i++){
            String value = "insertSample";
            //拼接字符串
            String number = Integer.toString(i);
            String result = value.concat(number);
            System.out.println(result);
            //拼接字符串  end
            LinkedHashMap sampleStorageList = (LinkedHashMap) map.get(result);
            Object collecId = (Object)sampleStorageList.get("collecId");
            Object countryCode = (Object)sampleStorageList.get("countryCode");
            Object userIdOperator = (Object)sampleStorageList.get("userIdOperator");
            Object holeLocCode = (Object)sampleStorageList.get("holeLocCode");
            Object storageQuantity = (Object)sampleStorageList.get("storageQuantity");
            //定义一个对象,并赋值
            SampleStorage sampleStorage = new SampleStorage();
            sampleStorage.setCollecId(Integer.parseInt(collecId.toString()));
            sampleStorage.setCountryCode(countryCode.toString());
            sampleStorage.setUserIdOperator(Integer.parseInt(userIdOperator.toString()));
            sampleStorage.setHoleLocCode(holeLocCode.toString());
            sampleStorage.setStorageQuantity(Double.parseDouble(storageQuantity.toString()));
            System.out.println(sampleStorage.toString());
            /**
             * 获取当前时间的8位字符串型
             */
            String year1="",month1="",date1="";
            Calendar c = Calendar.getInstance();
            int  year = c.get(Calendar.YEAR);
            int  month = c.get(Calendar.MONTH);
            month++;
            int   date = c.get(Calendar.DATE);
            year1=""+year;
            if(month<10){
                month1="0"+month;
            }else{
                month1=""+month;
            }
            if(date<10){
                date1="0"+date;
            }else{
                date1=""+date;
            }
            String time=year1+month1+date1;
            System.out.println("time"+time);
            /**
             * end
             * time为结果
             */
            //获取最大出库码
            String MaxStorageCode = sampleStorageDao.selectMaxStorageCode();
            System.out.println("最大出库码"+MaxStorageCode);
            //获取前8位
            String maxTime=MaxStorageCode.substring(0,8);
            String newStorageCode="";
            if(time.equals(maxTime)){//如果日期相同
                String maxNumber=MaxStorageCode.substring(8,MaxStorageCode.length());
                int intMaxNumber=Integer.parseInt(maxNumber);
                intMaxNumber++;
                newStorageCode=time+intMaxNumber;
            }else{
                newStorageCode=time+"1";
            }
            System.out.println("newStorageCode"+newStorageCode);
            //设置存储码
            sampleStorage.setStorageCode(newStorageCode);
            System.out.println("取出对象的存储码："+newStorageCode);
            /**
             * 执行插入
             */
            returnResult = sampleStorageDao.insertSampleStorage(sampleStorage);
            if(returnResult==0){
                hashMap.put("status","0");
                hashMap.put("msg","插入失败");
                //回滚
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return hashMap;
            }
        }
        hashMap.put("status","1");
        hashMap.put("msg","插入成功");
        return hashMap;
    }


}
